<template>
	<view>
		<u-swiper :list="slides" name="img_url" title height="320"></u-swiper>

		<u-tabs :list="list" :is-scroll="false" :current="currentSort" bar-width="60" :duration="0.3"
			@change="changeSort" class="u-m-t-30"></u-tabs>

		<u-row gutter="16" class="u-skeleton">
			<block v-if="goods[0].title">
			<u-col span="6" v-for="(item, index) in goods" :key="index">
				<goods-cards :item="item"></goods-cards>
			</u-col>
			</block>
			<view class="u-text-center" v-else >
				<u-loading mode="flower"></u-loading>
			</view>
		</u-row>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {

				currentSort: 0,
				list: [{
					name: '默认'
				}, {
					name: '热销'
				}, {
					name: '推荐'
				}, {
					name: '最新',
				}],
				slides: [],
				goods: [{},{},{},{}],
				page: 1,
				loading: false,
				onBottom: false
			}
		},
		onLoad() {
			// this.$u.utils.isLogin()
			this.getData();
		},
		methods: {
			changeSort(current) {
				this.currentSort = current
				this.goods = [{},{},{},{}]
				this.page = 1
				this.getData()
			},
			async getData(page) {
				// 显示骨架屏
				this.loading = true
				const params = {
					page: this.page
				}
				if (this.currentSort === 1) params.sales = 1;
				if (this.currentSort === 2) params.recommend = 1;
				if (this.currentSort === 3) params.new = 1;
				const res = await this.$u.api.index(params);
				// 隐藏骨架屏
				this.loading = false;
				this.goods = this.goods.pop().title ? [...this.goods, ...res.goods.data] : res.goods.data;
				this.slides = res.slides;
			}
		},
		onReachBottom() {
			// 重新请求数据，并且添加分页参数
			if (this.page < 5) {
				this.page = this.page + 1;
				this.getData({
					page: this.page
				})
			} else {
				this.onBottom = true;
				this.$u.toast('到底了')
			}

		}
	}
</script>
